package com.sgb.kjwl.viewmodel.business;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.business.ApprovalFlowEntity;

import com.sgb.kjwl.model.entity.business.CompanyEntity;
import com.sgb.kjwl.model.entity.business.CreateMemberEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.repository.DeviceRepertory;
import com.sgb.kjwl.view.ui.activity.business.AddMemberActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddMemberModel extends BaseViewModel {

    private AddMemberActivity activity;
    private DeviceRepertory repertory;

    public AddMemberModel(AddMemberActivity activity, DeviceRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void submit(CreateMemberEntity createMemberEntity){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBusinessManagementAPI().addMember(createMemberEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null){
                    return;
                }
                if (!response.body().getState().equals(APIManager.RESPONSE_OK)){
                    MToast.showToast(activity, response.body().getMsg());
                    return;
                }
                //跳转申请列表
                activity.finish();
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void search(String str){
        APIManager.getInstance().getBusinessManagementAPI().companyList(str).enqueue(new Callback<BaseEntity<ProListEntity<List<CompanyEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<CompanyEntity>>>> call, Response<BaseEntity<ProListEntity<List<CompanyEntity>>>> response) {
                if (response.body() == null || response.body().getData() == null || response.body().getData().getList() == null || response.body().getData().getList().size() == 0)
                    return;
                activity.showCompanyList(response.body().getData().getList());

            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<CompanyEntity>>>> call, Throwable t) {

            }
        });

    }

    //企业服务类型
    public void getCompanyTypes(){
        APIManager.getInstance().getBusinessManagementAPI().getCompanyTypes().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                if (response.body() == null || response.body().getState() == null){
                    return;
                }

                activity.setCompanyTypes(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {

            }
        });
    }

    //企业会员等级
    public void getMemberLevels(){
        APIManager.getInstance().getBusinessManagementAPI().getMemberLevels().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                if (response.body() == null || response.body().getState() == null){
                    return;
                }

                activity.setMemberLevels(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {

            }
        });
    }

    public void getFlow() {
        APIManager.getInstance().getBusinessManagementAPI().getMemberApproverList().enqueue(new Callback<BaseEntity<ApprovalFlowEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<ApprovalFlowEntity>> call, Response<BaseEntity<ApprovalFlowEntity>> response) {
                if (response.body() == null || response.body().getData() == null || response.body().getData().getApprover() == null || response.body().getData().getApprover().size() == 0){
                    activity.setBlank();
                    return;
                }
                activity.showApprovalFlow(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<ApprovalFlowEntity>> call, Throwable t) {

            }
        });
    }

}
